-module(quek_utf8).
-export([to_utf8/1, from_utf8/1, to_code/1, to_byte/1]).


to_utf8(Utf8) ->
    Utf8.


from_utf8(Utf8) ->
    Utf8.


to_code([B1|T]) ->
    case (B1 band 2#11111000) of
	2#11110000 ->
	    [B2, B3, B4|T2] = T,
	    Code = (B1 bsl 32) bor (B2 bsl 16) bor (B3 bsl 8) bor B4,
	    {Code, T2};
	2#11100000 ->
	    [B2, B3|T2] = T,
	    Code = (B1 bsl 16) bor (B2 bsl 8) bor B3,
	    {Code, T2};
	2#11000000 ->
	    [B2|T2] = T,
	    Code = (B1 bsl 8) bor B2,
	    {Code, T2};
	_ ->
	    {B1, T}
    end.


to_byte(Code) when Code < 16#100 ->
    [Code];
to_byte(Code) when Code < 16#10000 ->
    <<B1, B2>> = <<Code:16/big>>,
    [B1, B2];
to_byte(Code) when Code < 16#1000000 ->
    B1 = Code bsr 16,
    B2 = (Code bsr 8) band 16#FF,
    B3 = Code band 16#FF,
    [B1, B2, B3];
to_byte(Code) ->
    <<B1, B2, B3, B4>> = <<Code:32/big>>,
    [B1, B2, B3, B4].
